/**
 * @param {number[]} arr1
 * @param {number[]} arr2
 * @return {number[]}
 */
var relativeSortArray = function(arr1, arr2) {
  let res = []
  let temp = []
  let map = new Map()
  arr1.forEach(v => {
    map.has(v) ? map.get(v).push(v): map.set(v, [v])
  })
  arr2.forEach(v => {
    if (map.has(v)) {
      res.push(map.get(v))
      map.delete(v)
    }
  })
  for (let [key, value] in map.entries()) {
    temp.push(value)
  }
  temp.sort((a, b) => a - b)
  return [...res, ...temp]
};